 /******************/
 /* 2018.2.17 */
 /* www.bkoak.com */
 /* 数码管程序示例 */
 /******************/
#include <stc15f2k60s2.h>
#include <intrins.h>

#define uchar unsigned char
#define uint unsigned int

uchar code tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,
                    0x82,0xf8,0x80,0x90,0xbf,0x7f};
/*这里如果不加code，就会占用寄存器，允许读取和修改寄存器的值，
但是寄存器的数量有限，故我们在这里加上code，将其定义为代码的
类型，只能读取不能修改*/
uchar t = 0;
uchar c5, c6, c7, c8;
uint num = 2330;
										
void Timer0Init(void);
void display(uchar c5, uchar c6, uchar c7, uchar c8);
void Delay1ms();
										
void main()
{
	P2 = ((P2&0x1f)|0xa0);//不再重复解释，前两章已解释多次
	P0 = 0x00;

	Timer0Init();
	EA=1;
	ET0=1;
	
	while(1)
	{
		if(t == 200)
		{
			t = 0;
			num--;
			c5 = (num / 1000) % 10;
			c6 = (num / 100) % 10;
			c7 = (num /10) % 10;
			c8 = num % 10;
		}
		display(c5, c6, c7, c8);
	}
}

void display(uchar c5, uchar c6, uchar c7, uchar c8)
{
	P2 = ((P2&0x1f)|0xc0);
	P0 = 0x80;
	P2 = ((P2&0x1f)|0xff);
	P0 = tab[c8];
	Delay1ms();
	
	P2 = ((P2&0x1f)|0xc0);
	P0 = 0x40;
	P2 = ((P2&0x1f)|0xff);
	P0 = tab[c7];
	Delay1ms();
	
	P2 = ((P2&0x1f)|0xc0);
	P0 = 0x20;
	P2 = ((P2&0x1f)|0xff);
	P0 = tab[c6];
	Delay1ms();
	
	P2 = ((P2&0x1f)|0xc0);
	P0 = 0x10;
	P2 = ((P2&0x1f)|0xff);
	P0 = tab[c5];
	Delay1ms();
}

void Delay1ms()		//@11.0592MHz
{
	unsigned char i, j;

	_nop_();
	_nop_();
	_nop_();
	i = 11;
	j = 190;
	do
	{
		while (--j);
	} while (--i);
}

void Timer0Init(void)//5毫秒@11.0592MHz
{
   AUXR |= 0x80;//定时器时钟1T模式
   TMOD &= 0xF0;//设置定时器模式
   TL0 = 0x00;//设置定时初值
   TH0 = 0x28;//设置定时初值
   TF0 = 0;//清除TF0标志
   TR0 = 1;//定时器0开始计时
}

void Timer0() interrupt 1
{
   t++;
}




/*
#include <stc15f2k60s2.h>

#define uchar unsigned char
#define uint unsigned int

uchar code tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,
                    0x82,0xf8,0x80,0x90,0xbf,0x7f};
/*这里如果不加code，就会占用寄存器，允许读取和修改寄存器的值，
但是寄存器的数量有限，故我们在这里加上code，将其定义为代码的
类型，只能读取不能修改*/
/*
uchar t = 0,i;
										
void Timer0Init(void);
										
void main()
{
	P2 = ((P2&0x1f)|0xa0);//不再重复解释，前两章已解释多次
	P0 = 0x00;
	P2 = ((P2&0x1f)|0xc0);//打开位选选573
	P0 = 0xff;//选择8位数码管
	P2 = ((P2&0x1f)|0xff);//打开段选573

	Timer0Init();
	EA=1;
	ET0=1;
	
	while(1)
	{
		if(t == 100)
		{
			t = 0;
			P0 = tab[i];
			i++;
			if(i == 12)
				i = 0;
		}
	}
}

void Timer0Init(void)//5毫秒@11.0592MHz
{
   AUXR |= 0x80;//定时器时钟1T模式
   TMOD &= 0xF0;//设置定时器模式
   TL0 = 0x00;//设置定时初值
   TH0 = 0x28;//设置定时初值
   TF0 = 0;//清除TF0标志
   TR0 = 1;//定时器0开始计时
}

void Timer0() interrupt 1
{
   t++;
}
*/